//
//  VCViewController.m
//  Gunz
//
//  Created by Joggy on 16/8/23.
//  Copyright © 2016年 NUPT. All rights reserved.
//

#import "VCViewController.h"
#import "D3RecordButton.h"
#import "QYNotice.h"

@interface VCViewController () <D3RecordDelegate> {
    
    D3RecordButton *record;
    
}

@end

@implementation VCViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self prepareForNav];
    [self prepareForLayout];
}

- (void)prepareForNav {
    [super prepareForNav];
    self.title = @"语音";
    //导航左按键
    UIImage *image = [UIImage imageNamed:@"all_back"];
    UIButton *leftBarButton = [UIButton buttonWithType:UIButtonTypeSystem];
    leftBarButton.frame = CGRectMake(0, 0, image.size.width, image.size.height);
    leftBarButton.layer.position = CGPointMake(22, 22);
    [leftBarButton setImage:[image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [leftBarButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBarButton];
}

- (void)prepareForLayout {
    [super prepareForLayout];
    record = [[D3RecordButton alloc] initWithFrame:CGRectMake(0, 0, 120, 66)];
    [record initRecord:self maxtime:120 title:@"上划取消录音"];
    [record setTitle:@"按住录音" forState:UIControlStateNormal];
    [record setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    record.backgroundColor = CUSTOM_GREEN;
    record.layer.cornerRadius = 6;
    record.center = CGPointMake(screenWidth/2, screenHeight*2/3);
    [self.view addSubview:record];
}

- (void)endRecord:(NSData *)voiceData {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"是否保存所采集的数据" message:@"" preferredStyle:UIAlertControllerStyleAlert];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"保存" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //保存数据
        UIAlertController *saveAlert = [UIAlertController alertControllerWithTitle:@"数据名" message:@"" preferredStyle:UIAlertControllerStyleAlert];
        [saveAlert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
            textField.placeholder = @"请输入数据名";
        }];
        [saveAlert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            UITextField *textField = saveAlert.textFields[0];
            NSString *superPath = [NSString stringWithFormat:@"%@/%@", documentBasePath, GZUserAudioPath];
            NSFileManager *manager = [NSFileManager defaultManager];
            if(![manager fileExistsAtPath:superPath]) {
                [manager createDirectoryAtPath:superPath withIntermediateDirectories:true attributes:nil error:nil];
            }
            if([textField.text isEqualToString:@""]) {
                textField.text = [GZConst getCurrentDateFormate];
            }
            NSString *savePath = [NSString stringWithFormat:@"%@/%@/%@.mp3", documentBasePath, GZUserAudioPath, textField.text];
            if([voiceData writeToFile:savePath atomically:true]) {
//                [self dismissViewControllerAnimated:true completion:nil];
            }
            else {
                [QYNotice showError:@"保存失败" toView:self.view];
                [QYNotice dismissWithDelay:3];
            }
        }]];
        [saveAlert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
        [self presentViewController:saveAlert animated:true completion:nil];
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"不保存" style:UIAlertActionStyleCancel handler:nil]];
    [self presentViewController:alert animated:true completion:nil];
}

- (void)dragEnter {
    [record setTitle:@"松开发送" forState:UIControlStateNormal];
}

- (void)dragExit {
    [record setTitle:@"按住录音" forState:UIControlStateNormal];
}

- (void)back {
    [self dismissViewControllerAnimated:true completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
